package com.bw.zg3park2212.repair

import android.annotation.SuppressLint
import android.app.ActionBar.LayoutParams
import android.content.Intent
import android.text.method.LinkMovementMethod
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.PopupWindow
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.entity.RepairAddParam
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.adpter.ImageAdpter
import com.bw.zg3park2212.adpter.RepairAdpter
import com.bw.zg3park2212.databinding.ActivityRepairBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = "/app/RepairActivity")
class RepairActivity : BaseActivity<ActivityRepairBinding,RepairViewModel>() {
    lateinit var repairAdpter: RepairAdpter
    var order=""
    lateinit var imageAdpter: ImageAdpter
    @SuppressLint("MissingInflatedId")
    override fun intitView() {
        repairAdpter = RepairAdpter()
        binding.rvWxgl.adapter = repairAdpter
        binding.rvWxgl.layoutManager=LinearLayoutManager(this)
       // viewmodel.repair()
        binding.repairOrder.setOnClickListener {
            if (order.isEmpty()){
                order="1"
                binding.repairOrderiv.setImageResource(R.drawable.baseline_arrow_upward_24)
                binding.repairOrdertv.setText("升序")
                repairAdpter.data.sortBy {
                    it.repairTime
                }
            }else{
                order=""
                binding.repairOrderiv.setImageResource(R.drawable.baseline_arrow_downward_24)
                binding.repairOrdertv.setText("降序")
                repairAdpter.data.sortByDescending {
                    it.repairTime
                }
            }
            repairAdpter.notifyDataSetChanged()
        }
        binding.sv.setOnQueryTextListener(object:SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (query != null) {
                    viewmodel.repairName(query)
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                viewmodel.repair()
                return false
            }
        })
        binding.repairFilter.setOnClickListener {
            val popupWindow=PopupWindow(this)
            val inflate = LayoutInflater.from(this).inflate(R.layout.item_popreair, null)
            val btnYes = inflate.findViewById<Button>(R.id.btn_yes)
            val btnNo = inflate.findViewById<Button>(R.id.btn_no)
            val repairorderNumber = inflate.findViewById<EditText>(R.id.repair_orderNumber)
            btnYes.setOnClickListener {
                viewmodel.repaircode(repairorderNumber.text.toString())
            }
            btnNo.setOnClickListener {
                viewmodel.repair()
                popupWindow.dismiss()
            }
            popupWindow.isFocusable=true
            popupWindow.isOutsideTouchable=true
            popupWindow.width=LayoutParams.MATCH_PARENT
            popupWindow.height=300
            popupWindow.contentView=inflate
            popupWindow.showAtLocation(binding.repairFilter, Gravity.BOTTOM,0,0)
        }

        binding.add.setOnClickListener {
            val popupWindow=PopupWindow(this)
            val inflate = LayoutInflater.from(this).inflate(R.layout.item_pop_repairadd, null)
            val btnYes = inflate.findViewById<Button>(R.id.btn_yes)
            val btnNo = inflate.findViewById<Button>(R.id.btn_no)
            val orderNumber = inflate.findViewById<EditText>(R.id.add_orderNumber)
            val repairName = inflate.findViewById<EditText>(R.id.add_repairName)
            val contactMethod = inflate.findViewById<EditText>(R.id.add_contactMethod)
            val faultDescription = inflate.findViewById<EditText>(R.id.add_faultDescription)
            val processingStatus = inflate.findViewById<EditText>(R.id.add_processingStatus)
            val addphoto = inflate.findViewById<ImageView>(R.id.add_photo)
            val addrvimage = inflate.findViewById<RecyclerView>(R.id.add_rvimage)
            imageAdpter=ImageAdpter()
            addrvimage.adapter=imageAdpter
            addrvimage.layoutManager=LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
            addphoto.setOnClickListener {
                if (imageAdpter.data.size==4){
                    ToastUtils.showLong("最多上传4张图片")
                    return@setOnClickListener
                }else{
                    PictureSelector.create(this,100).selectPicture()
                }
            }
            btnYes.setOnClickListener {
                val repiaraddparam=RepairAddParam()
                repiaraddparam.orderNumber=orderNumber.text.toString()
                repiaraddparam.repairName=repairName.text.toString()
                repiaraddparam.contactMethod=contactMethod.text.toString().toLong()
                repiaraddparam.faultDescription=faultDescription.text.toString()
                repiaraddparam.processingStatus=processingStatus.text.toString()
                repiaraddparam.repairTime=SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
                viewmodel.repairAdd(repiaraddparam)
            }
            btnNo.setOnClickListener {
                popupWindow.dismiss()
            }
            popupWindow.isFocusable=true
            popupWindow.isOutsideTouchable=true
            popupWindow.width=LayoutParams.MATCH_PARENT
            popupWindow.height=1000
            popupWindow.contentView=inflate
            popupWindow.showAtLocation(binding.repairFilter, Gravity.BOTTOM,0,0)
        }
        ItemTouchHelper(object :ItemTouchHelper.Callback(){
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
            ): Int {
                return makeMovementFlags(0,ItemTouchHelper.LEFT)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder,
            ): Boolean {
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.layoutPosition
                viewmodel.repairDal(repairAdpter.data[position].id)
                //ToastUtils.showLong(repairAdpter.data[position].id)
                repairAdpter.notifyItemRemoved(position)
            }
        }).attachToRecyclerView(binding.rvWxgl)
        repairAdpter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/RepairXiangActivity")
                .withSerializable("rxid",repairAdpter.data[position])
                .navigation()
        }
    }

    override fun intitData() {
        viewmodel.repairDalSuccse.observe(this){
            ToastUtils.showLong("删除成功")
        }
        viewmodel.repairAddSuccse.observe(this){
            ToastUtils.showLong("添加成功")
            viewmodel.repair()
        }
        viewmodel.repairCodeSuccse.observe(this){
            ToastUtils.showLong("查询成功")
            repairAdpter.data.clear()
            repairAdpter.addData(it)
            repairAdpter.notifyDataSetChanged()
        }
        viewmodel.repairNameSuccse.observe(this){
            ToastUtils.showLong("查询成功")
            repairAdpter.data.clear()
            repairAdpter.addData(it)
            repairAdpter.notifyDataSetChanged()
        }
        viewmodel.repairSuccse.observe(this){
            repairAdpter.data.clear()
            repairAdpter.addData(it)
            repairAdpter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        viewmodel.repair()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==100&&resultCode== RESULT_OK){
            val bean = data!!.extras!!.get(PictureSelector.PICTURE_RESULT) as PictureBean
            imageAdpter.data.add(bean.path)
            imageAdpter.notifyDataSetChanged()
        }
    }

}